<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--监区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入监区名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            @node-click="handleNodeClick"
            ref="tree"
            default-expand-all
          />
        </div>
      </el-col>
      <!--人员数据-->
      <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:criminal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:criminal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:criminal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:criminal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="criminalList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label-width="100px" label="民族">
              <span>{{ props.row.nation }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="籍贯">
              <span>{{ props.row.nativePlace }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="文化程度">
              <span>{{ props.row.educationLevel }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="入监时间">
              <span>{{ props.row.inPrisonTime }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="婚姻状况">
              <span>{{ props.row.isMarriage }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="就业状况">
              <span>{{ props.row.employment }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="进的次数">
              <span>{{ props.row.recidivist }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="刑期开始时间">
              <span>{{ props.row.prisonTermStartTime }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="刑期结束时间">
              <span>{{ props.row.prisonTermEndTime }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="最近一次犯罪">
              <span>{{ props.row.latestCrime }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="疾病情况">
              <span>{{ props.row.disease }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="送监单位">
              <span>{{ props.row.sourceOrg }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="刑期">
              <span>{{ props.row.prisonTerm }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="存款">
              <span>{{ props.row.deposit }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="人员编码">
              <span>{{ props.row.code }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="是否重点人员">
              <span>{{ props.row.isStress == '0'?'否':'是' }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="备注">
              <span>{{ props.row.remarks }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="宿舍区域ID">
              <span>{{ props.row.areaId }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="手环ID">
              <span>{{ props.row.braceletId }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="ic卡号">
              <span>{{ props.row.icCard }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="状态">
              <span>{{ props.row.currentStatus }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="是否在所">
              <span>{{ props.row.isInPlace == 0?'否':'是' }}</span>
            </el-form-item>
            <el-form-item label-width="100px" label="毒品">
              <span>{{ props.row.drug }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="头像" align="center" prop="headimgId">
        <template slot-scope="scope">
          <img :src="downloadUrl+'?id='+scope.row.headimgId" style="width:80px;height:80px">
        </template>
      </el-table-column>
      <el-table-column label="头像路径" align="center" prop="imgPath" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="所属单位编号" align="center" prop="orgId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:criminal:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:criminal:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      </el-col>

    <!-- 添加或修改犯罪人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="图片" prop="headimgId">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :with-credentials="true"
            :data="files"
            :headers="importHeaders"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="关联用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入关联用户ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option key="男" label="男" value="男"></el-option>
            <el-option key="女" label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="所属单位编号" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入所属单位编号" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-date-picker format="yyyy-MM-dd HH:mm:ss"
            v-model="form.birthday"
            type="datetime"
            placeholder="请输入生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="文化程度" prop="educationLevel">
          <el-input v-model="form.educationLevel" placeholder="请输入文化程度" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="入监时间" prop="inPrisonTime">
          <el-date-picker format="yyyy-MM-dd HH:mm:ss"
            v-model="form.inPrisonTime"
            type="datetime"
            placeholder="请输入入监时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否结婚" prop="isMarriage">
          <el-select v-model="form.isMarriage" placeholder="请选择">
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="就业状况" prop="employment">
          <el-input v-model="form.employment" placeholder="请输入就业状况" />
        </el-form-item>
        <el-form-item label="刑期开始时间" prop="prisonTermStartTime">
          <el-date-picker format="yyyy-MM-dd HH:mm:ss"
            v-model="form.prisonTermStartTime"
            type="datetime"
            placeholder="请输入刑期开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="刑期结束时间" prop="prisonTermEndTime">
          <el-date-picker format="yyyy-MM-dd HH:mm:ss"
            v-model="form.prisonTermEndTime"
            type="datetime"
            placeholder="请输入刑期结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="惯犯次数" prop="recidivist">
          <el-input v-model="form.recidivist" placeholder="请输入惯犯次数" />
        </el-form-item>
        <el-form-item label="最近一次犯罪" prop="latestCrime">
          <el-input v-model="form.latestCrime" placeholder="请输入最近一次犯罪" />
        </el-form-item>
        <el-form-item label="疾病情况" prop="disease">
          <el-input v-model="form.disease" placeholder="请输入疾病情况" />
        </el-form-item>
        <el-form-item label="送监单位" prop="sourceOrg">
          <el-input v-model="form.sourceOrg" placeholder="请输入送监单位" />
        </el-form-item>
        <el-form-item label="刑期" prop="prisonTerm">
          <el-input v-model="form.prisonTerm" placeholder="请输入刑期" />
        </el-form-item>
        <el-form-item label="存款" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入存款" />
        </el-form-item>
        <el-form-item label="人员编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入人员编码" />
        </el-form-item>
        <el-form-item label="是否重点人员" prop="isStress">
          <el-select v-model="form.isStress" placeholder="请选择">
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="宿舍区域" prop="areaId">
          <treeselect v-model="form.areaId" :options="deptOptions" placeholder="请选择分类" style="width:200px"/>
        </el-form-item>
        <el-form-item label="ic卡号" prop="icCard">
          <el-input v-model="form.icCard" placeholder="请输入ic卡号" />
        </el-form-item>
        <el-form-item label="头像ID" prop="headimgId">
          <el-input v-model="form.headimgId" placeholder="请输入头像ID" />
        </el-form-item>
        <el-form-item label="手环ID" prop="braceletId">
          <el-input v-model="form.braceletId" placeholder="请输入手环ID" />
        </el-form-item>
        <el-form-item label="头像路径" prop="imgPath">
          <el-input v-model="form.imgPath" placeholder="请输入头像路径" />
        </el-form-item>
        <el-form-item label="是否关注" prop="isFollow">
          <el-select v-model="form.isFollow" placeholder="请选择">
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否在所" prop="isInPlace">
          <el-select v-model="form.isInPlace" placeholder="请选择">
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="0" label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="updateState">
          <el-select v-model="form.updateState" placeholder="请选择">
            <el-option key="1" label="已更新" value="1"></el-option>
            <el-option key="0" label="未更新" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="毒品" prop="drug">
          <el-input v-model="form.drug" placeholder="请输入毒品" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-row>
  </div>
</template>

<script>
import { listCriminal, getCriminal, delCriminal, addCriminal, updateCriminal } from "@/api/system/criminal";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Criminal",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 犯罪人员信息表格数据
      criminalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 监区树选项
      deptOptions: undefined,
      // 监区名称
      deptName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      files: {'module':'CRIMINAL'},
      imageUrl: '',
      uploadUrl: process.env.VUE_APP_BASE_API + '/system/files',
      downloadUrl: process.env.VUE_APP_BASE_API + '/system/files/download',
      importHeaders: {Authorization: 'Bearer ' + getToken()},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        sex: null,
        idCard: null,
        orgId: null,
        birthday: null,
        nation: null,
        nativePlace: null,
        educationLevel: null,
        address: null,
        inPrisonTime: null,
        isMarriage: null,
        employment: null,
        prisonTermStartTime: null,
        prisonTermEndTime: null,
        recidivist: null,
        latestCrime: null,
        disease: null,
        sourceOrg: null,
        prisonTerm: null,
        deposit: null,
        code: null,
        isStress: null,
        remarks: null,
        areaId: null,
        icCard: null,
        headimgId: null,
        braceletId: null,
        imgPath: null,
        currentStatus: null,
        isFollow: null,
        isInPlace: null,
        updateState: null,
        sort: null,
        drug: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询犯罪人员信息列表 */
    getList() {
      this.loading = true;
      listCriminal(this.queryParams).then(response => {
        this.criminalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        sex: null,
        idCard: null,
        orgId: null,
        birthday: null,
        nation: null,
        nativePlace: null,
        educationLevel: null,
        address: null,
        inPrisonTime: null,
        isMarriage: null,
        employment: null,
        prisonTermStartTime: null,
        prisonTermEndTime: null,
        recidivist: null,
        latestCrime: null,
        disease: null,
        sourceOrg: null,
        prisonTerm: null,
        deposit: null,
        code: null,
        isStress: null,
        remarks: null,
        createTime: null,
        updateTime: null,
        areaId: null,
        icCard: null,
        headimgId: null,
        braceletId: null,
        imgPath: null,
        currentStatus: 0,
        isFollow: null,
        isInPlace: null,
        updateState: null,
        sort: null,
        drug: null
      };
      this.resetForm("form");
    },
    /** 查询监区下拉树结构 */
    getTreeselect() {
      treeselect({excludeTypes: '1,3'}).then((response) => {
        this.deptOptions = response.data;
      });
    },    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaId = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加犯罪人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCriminal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改犯罪人员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCriminal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCriminal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除犯罪人员信息编号为"' + ids + '"的数据项？').then(function() {
        return delCriminal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/criminal/export', {
        ...this.queryParams
      }, `criminal_${new Date().getTime()}.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.headimgId = res.data;
    },
    beforeAvatarUpload(file) {
      this.files = {
        id: null,
        fileName: file.name,
        fileType: file.type,
        fileSize: file.size,
        fileModule: 'CRIMINAL'
      };
    },
  }
};
</script>
<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>